$(function() {
	//显示菜单信息
	var head = $("#breadcrumbs");
	head.html('<ul class="breadcrumb">' +
		'<li><i class="icon-home"></i> <a href="/">首页</a><span class="divider"><i class="icon-angle-right"></i></span></li>' +
		'<li><a href="#">统计分析</a> <span class="divider"><i class="icon-angle-right"></i></span></li>' +
		'<li><a href="/analyze">绩效统计</a><span class="divider"><i class="icon-angle-right"></i></span></li>' +
		'<li class="active">员工绩效详细信息</li></ul>');

	var init = function(empId, time) {
		$.ajax({
			url: '/api/performance/selPerfDetail',
			type: 'get',
			data: {'empId': empId, 'perfDate': time, '_t':new Date().getTime()},
			success: function(data) {
				// console.info(data);
				if (data != null && data.perfData != null && data.perfData.length != 0) {
					var perfData = data.perfData;
					// perfDeprId = perfData.departmentId;
					// perfState=perfData.state
					var termsData = data.termsData;
					showBasic(perfData);
					// $("#perfDate").val(perfData.perfDate);
					// $("#employee").val(perfData.empName);
					// $("#employeeid").val(perfData.employeeId);
					// $("#assessmentid").val(perfData.assessmentPersonId);
					// $("#assessmentPerson").val(perfData.assName);
					// $("#comment").val(perfData.comment);
					// $("#perfScore").html(perfData.score);
					// $("#perfLevel").html(perfData.level);

					// getOrgParents(perfData.departmentId, 0);
					// if (perfData.preDeptId != null) {
					// 	$("#preDeptId").val(perfData.preDeptId);
					// 	getOrgParents(perfData.preDeptId, 1)
					// }
					// if (perfData.project != null) {
					// 	$("#project").val(perfData.project);
					// }
					showTerms(termsData, perfData.state);
					if (data.logData != null && data.logData != "") {
						showLogs(data.logData);
						// $("#logTab").css("display", "block");
					}

				} else {
					return;
				}
			}
		});
	};
	var key = $("#getKey").val();
	// console.info("key", $("#getKey").val());
	key = JSON.parse(decodeURIComponent(key));
	init(key.empId, key.time);

	var getOrgParents = function(orgId, type) {
		$.ajax({
			url: '/api/organizations/getParents/' + orgId + '?',
			type: 'get',
			data: {'_t': new Date().getTime()},
			success: function(result) {
				// console.info(result);
				var parentString = "";
				for (var i = result.length - 1; i >= 0; i--) {
					if (i != 0) {
						if (result[i].orgName == "分公司") {
							continue;
						} else {
							parentString += result[i].orgName + "/";
						}

					} else {
						parentString += result[i].orgName;
					}
				}
				if (type == 1) {
					$("#projectDept").val(parentString);
					$("#preDeptId").val(result[0].id);
				} else {
					$("#department").val(parentString);
					$("#deptId").val(result[0].id);
				}


			}
		});
	};

    var showBasic = function(perfData) {
    	$("#perfDate").val(perfData.perfDate);
    	$("#employee").val(perfData.empName);
    	$("#employeeid").val(perfData.employeeId);
    	$("#assessmentid").val(perfData.assessmentPersonId);
    	$("#assessmentPerson").val(perfData.assName);
    	$("#comment").val(perfData.comment);
    	$("#perfScore").html(perfData.score);
    	$("#perfLevel").html(perfData.level);

    	getOrgParents(perfData.departmentId, 0);
    	if (perfData.preDeptId != null) {
    		$("#preDeptId").val(perfData.preDeptId);
    		getOrgParents(perfData.preDeptId, 1)
    	}
    	if (perfData.project != null) {
    		$("#project").val(perfData.project);
    	}
    };
	
	var showTerms = function(termsData, perfState) {
		if (termsData != null && termsData.length != 0) {
			$("#tbd_task").html('');
			if (perfState == 1) {
				$("#tbd_pluse").html('');
				$("#tbd_professional").html('');
			}
			var showPluseFlag=true;
			var showProfessionalFlag=true;
			$.each(termsData, function(i, item) {
				// console.info(i + "-----", item.completion);
				var templateData = {
					term: item.term,
					termId: item.id,
					content: item.content,
					weight: item.weight + "%",
					completion: item.completion,
					score: item.score,
					name: item.name,
					assPersonId: item.assPersonId
				};
				var template;


				if (item.type == 0) {
					//工作事项

					var returnTemplate = "<label style='margin:6px' name={{assPersonId}}><input type=checkbox id='termId' value={{termId}}><span class=lbl style='padding-bottom:6px'></span><input type=hidden id='assperson' value={{assPersonId}}><span id='assName'>{{name}}</span></label>&nbsp;&nbsp;&nbsp;";
					var $returnHtml = $(Mustache.to_html(returnTemplate, templateData));
					$("#returnBack").append($returnHtml);

					template = "<tr><input type='hidden' id=termId name=termId value='{{termId}}'><td class=span3 style='width:20%;word-wrap:break-word;vertical-align:middle; text-align:center' id=term name=term>{{term}}</td>" +
						"<td class='span3' style='width:30%;word-wrap:break-word;vertical-align:middle; text-align:center' id=content name=content>{{content}}</td>" +
						"<td class='span2 hidden-480' style='width:10%;word-wrap:break-word;vertical-align:middle; text-align:center'  name=weight >{{weight}}</td>" +
						"<td class='span3 hidden-phone' style='width:20%;word-wrap:break-word;vertical-align:middle; text-align:center' id='completion' name='completion'>{{completion}}</td>" +
						"<td class='span2 hidden-480' style='width:10%;word-wrap:break-word;vertical-align:middle; text-align:center' id='score' name='score'>{{score}}</td>" +
						"<td class='span2 hidden-480' style='width:10%;word-wrap:break-word;vertical-align:middle; text-align:center' id='assName' name='assName' >{{name}}</td></tr>";
					var $html = $(Mustache.to_html(template, templateData));
					$("#tbd_task").append($html);

				} else if (item.type == 1) {
					showPluseFlag=false;
					template = "<tr><input type='hidden' id=pluse_termId name=pluse_termId value='{{termId}}'><td class='span3' style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;' id='pluse_term' name='pluse_term'>{{term}}</td>" +
						"<td class='span3' style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;' id='pluse_content' name='pluse_content'>{{content}}</td>" +
						"<td class='span2 hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;' id='pluse_score' name='pluse_score'> {{score}}</td>" +
						"<td class='span2' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;' id='pluse_name' name='pluse_score'>{{name}}</td>" +
						"</tr>";

					var $html = $(Mustache.to_html(template, templateData));
					$("#tbd_pluse").append($html);
				} else if (item.type == 2) {
					showProfessionalFlag=false;
					template = "<tr><input type=hidden id=professional_termId name=professional_termId value='{{termId}}'><td class='span3' style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;' id='professional_term' name='professional_term'>{{term}}</td>" +
						"<td class='span3' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;' id='professional_weight' name='professional_weight'>{{weight}}</td>" +
						"<td class='span2 hidden-480' style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;' id='professional_completion' name='professional_completion'>{{completion}}</td>" +
						"<td class='span2 hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;' name='professional_score' id='professional_score'>{{score}}</td></tr>";


					var $html = $(Mustache.to_html(template, templateData));
					$("#tbd_professional").append($html);
				}
			});
			if(showPluseFlag){
				$("#pluseDiv").remove();
			}
			if(showProfessionalFlag){
				$("#professionalDiv").remove();
			}
			
		} else {
			return;
		}

	};

	//点击返回
	$("#goBack").click(function() {
		var key = $("#getKey").val();
		window.location.href = '/analyze?key=' + encodeURIComponent(key);
	});

    //历史记录列表
	var showLogs = function(logData) {
		// console.info("logData", logData);
		$("#tbd_log").html('');

		$.each(logData, function(i, item) {
			// alert(item.updateDate);
			var templateData = {
				logId: item.id,
				name: item.name,
				updateDate: item.updateDate,
				operation: item.operation
			};
			var template = "<tr><td class='center'>{{name}}<input type=hidden id=logId value={{logId}}></td><td class='center'>{{updateDate}}</td><td class='center'>{{operation}}</td></tr>";
			var $html = $(Mustache.to_html(template, templateData));
			$html.click(function() {
				// window.location.href = "/api/log/" + item.id;
				$.ajax({
					url: "/api/log/" + item.id,
					type: "get",
					success: function(logData) {
						// console.info(logData);
						$("#tbd_task").html('');
						$("#tbd_pluse").html('');
						$("#tbd_professional").html('');
						$("#perfScore").html('0');
						$("#perfLevel").html('E');
						
						$("#logTab").removeClass("active");
						$("#log").removeClass("active");
						$("#demoPerf").addClass("active");
						$("#performanceInfo").addClass("active");
						if(logData != null && logData.content.perfData != null) {
							showBasic(logData.content.perfData);
							if(logData.content.termsData != null) {
								showTerms(logData.content.termsData, logData.content.perfData.state);
							}
							// console.info("logTab",$("#logTab").hasClass("active"));
							
						}
					}
				});
			});
			$("#tbd_log").append($html);
		});
	};


});